{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using `music21` with the Jupyter (iPython) Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting up the Jupyter environment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get `music21` to work it's best to have a good pip installation first.  Then you should be able to simply install jupyter (and IPython if you don't have it):"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```$ sudo pip install jupyter\n",
    "$ sudo pip install ipython```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using the web-based Jupyter Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Start jupyter in a directory you want to save files by typing."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "```$ jupyter notebook```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now in the webbrowser you can type commands such as the ones below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from music21 import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = chord.Chord(\"C4 E4 G4\")\n",
    "c.isConsonant()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And that is basically it!  We use `sphinx` for interwiki links, which, sadly, won't show up on the notebook, but should render properly.\n",
    "\n",
    "But I did make a sphinx-markdown Notebook extension.  When it's ready, I'll put it up. Make a directory \"sphinx-markdown\" in a \"usability\" directory in \"nbextensions\" (mine is at \"~/Library/Jupyter/nbextensions\"). And put this file there as \"main.js\": [https://gist.github.com/mscuthbert/6411762f98f3536712e0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check to see if it exists:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import notebook\n",
    "notebook.nbextensions.check_nbextension('usability/sphinx-markdown', user=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load it with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Enabling notebook extension usability/sphinx-markdown/main...\n",
      "      - Validating: \u001b[32mOK\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "E = notebook.nbextensions.EnableNBExtensionApp()\n",
    "E.toggle_nbextension('usability/sphinx-markdown/main')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Save, and reload.  Then test it with a markdown cell like:"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "For more information, see, :ref:`installing IPython/Jupyter for Music21 <installJupyter>`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "sphinx_links": {
     "any": true
    }
   },
   "source": [
    "And you should get this output:\n",
    "\n",
    "  For more information, see, :ref:`installing IPython/Jupyter for Music21 <installJupyter>`. :-)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It works!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
